home *** CD-ROM | disk | FTP | other *** search
- Path: mail2news.demon.co.uk!genesis.demon.co.uk
- From: Lawrence Kirby <fred@genesis.demon.co.uk>
- Newsgroups: comp.lang.c
- Subject: Re: What happens on realloc() failure?
- Date: Sun, 24 Mar 96 17:34:06 GMT
- Organization: none
- Message-ID: <827688846snz@genesis.demon.co.uk>
- References: <Pine.SOL.3.91-941213.960322192958.24127A-100000@aidan.ncl.ac.uk> <4ivnfiINNdib@keats.ugrad.cs.ubc.ca>
- Reply-To: fred@genesis.demon.co.uk
- X-NNTP-Posting-Host: genesis.demon.co.uk
- X-Newsreader: Demon Internet Simple News v1.27
- X-Mail2News-Path: genesis.demon.co.uk
-
- In article <4ivnfiINNdib@keats.ugrad.cs.ubc.ca>
- c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
-
- >In article <Pine.SOL.3.91-941213.960322192958.24127A-100000@aidan.ncl.ac.uk>,
- >Tom Seddon <T.W.Seddon@ncl.ac.uk> wrote:
- > >I am writing a program which will need to call realloc() a fair amount.
- > >What I would like to know is whether the original data is still usable if
- > >the realloc() fails. (Although it is unlikely that there will be problems
- > >with running out of memory, it is possible that there will be problems due
- > >to fragmentation of memory.)
- >
- >Read some reliable documentation (ISO standard, K&R2). If you had the K&R, you
- >would know from page 252 that:
- >
- > realloc returns a pointer to the new space, or NULL if the
- > request cannot be satisfied in which case [the pointer parameter]
- > is not changed.
-
- I assume you meant by [the pointer parameter] the object that the pointer
- argument pointed to.
-
- > >Since realloc() returns NULL on failure, is it safe to use the pointer to
- > >the malloc()ed area I was trying to resize?
- >
- >Apparently so.
-
- For a more confident statement the standard itself says:
-
- "If the space cannot be allocated, the object pointed to by ptr is unchanged."
-
- >
- > >Or will the old area have
- > >been freed by free() and now be unusable? What does the ANSI standard
- > >have to say about this? I've never seen anything about this point before
-
- When realloc fails the old object still exists and you should still
- free it when you have finished with it. So simply ignoring it after realloc
- fails is essentially a memory leak.
-
- >Presumably what I just said, since the K&R2 is quite faithful to the standard.
-
- And now we have the proof (at least in this case)! :-)
-
- --
- -----------------------------------------
- Lawrence Kirby | fred@genesis.demon.co.uk
- Wilts, England | 70734.126@compuserve.com
- -----------------------------------------
-